package com.skyline.user.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.skyline.base.dao.impl.BaseDaoImpl;
import com.skyline.base.type.Activity;
import com.skyline.common.bean.Page;
import com.skyline.user.dao.FanDao;
import com.skyline.user.mapper.FanMapper;
import com.skyline.user.model.Fan;
import com.skyline.user.type.IdolType;

@Repository("fanDao")
public class FanDaoImpl extends BaseDaoImpl implements FanDao {
	/**
	 * insert into fan
	 * (idolid,idolnickname,idolportrait,fanid,fannickname,fanportrait
	 * ,idoltype,createtime) values (?,?,?,?,?,?,?,current_timestamp)
	 */
	@Value("${FanDao.insertFan}")
	private String insertFanSql;

	/**
	 * update fan set activity=? where idolid=? and fanid=?
	 */
	@Value("${FanDao.updateFanActivity}")
	private String updateFanActivitySql;

	// @Value("${FanDao.updateFanAttentionType}")
	// private String updateFanAttentionTypeSQL;

	/**
	 * select * from fan where idolid=? and activity=?
	 */
	@Value("${FanDao.queryFansByIdolId}")
	private String queryFansByIdolIdSql;

	/**
	 * select * from fan where fanid=? and activity=?
	 */
	@Value("${FanDao.queryIdolsByFanId}")
	private String queryIdolsByFanIdSql;

	/**
	 * select * from fan where fanid=? and idoltype=? and activity= ?
	 */
	@Value("${FanDao.queryIdolsByFanIdAndIdolType}")
	private String queryIdolsByFanIdAndIdolTypeSql;

	/**
	 * select t1.* from fan t1,fan t2 where t1.fanId=? and t2.fanId=? and
	 * t1.idolId=t2.idolId and t1.activity=?
	 */
	@Value("${FanDao.queryBothIdols}")
	private String queryBothIdolsSql;

	/**
	 * select t1.* from fan t1,fan t2 where t1.fanId=? and t2.fanId=? and
	 * t1.idolId=t2.idolId and t1.idolType=? and t1.activity=?
	 */
	@Value("${FanDao.queryBothIdolsByIdolType}")
	private String queryBothIdolsByIdolTypeSql;

	/**
	 * select t4.*, case when (t3.idolId=t4.idolId and t3.t2act='NORMAL') then
	 * true else false end as followed from fan t4 left join (select
	 * t1.*,t2.activity t2act from fan t1,fan t2 where t1.fanId=? and t2.fanId=?
	 * and t1.idolId=t2.idolId) t3 on t3.idolId=t4.idolId where t4.fanId=? and
	 * t4.idolType=? and t4.activity=?
	 */
	@Value("${FanDao.queryIdolsByIdolTypeWithFollowed}")
	private String queryIdolsByIdolTypeWithFollowedSql;

	/**
	 * select t4.*, case when t3.t2act='NORMAL' then true else false end as
	 * followed from fan t4 left join (select t1.*,t2.activity t2act from fan
	 * t1,fan t2 where t1.fanId=? and t2.fanId=? and t1.idolId=t2.idolId) t3 on
	 * t3.idolId=t4.idolId where t4.fanId=? and t4.activity=?
	 */
	@Value("${FanDao.queryIdolsWithFollowed}")
	private String queryIdolsWithFollowedSql;

	/**
	 * select t4.*, case when t3.t2act='NORMAL' then true else false end as
	 * followed from fan t4 left join (select t1.*,t2.activity t2act from fan
	 * t1,fan t2 where t1.fanId=t2.idolId and t1.idolId=? and t2.fanId=?) t3 on
	 * t3.idolId=t4.idolId where t4.idolId=? and t4.activity=?
	 */
	@Value("${FanDao.queryFansWithFollowed}")
	private String queryFansWithFollowedSql;

	/**
	 * select t1.* from fan t1,fan t2 where t1.fanId=t2.idolId and
	 * t1.idolId=t2.fanId and t1.fanId=? and t4.activity=?
	 */
	@Value("${FanDao.queryEachOtherIdols}")
	private String queryEachOtherIdolsSql;

	/**
	 * select count(1) from fan where idolid=? and fanId=? and activity=?
	 */
	@Value("${FanDao.countFans}")
	private String countFandsSql;

	// TODO 暂不支持Cache，又要另外研究
	public void insertFan(Long idolId, String idolNickname, String idolPortrait, Long fanId, String fanNickname,
			String fanPortrait, IdolType idolType) {
		jdbcTemplate.update(insertFanSql, idolId, idolNickname, idolPortrait, fanId, fanNickname, fanPortrait,
				idolType.toString());
	}

	// TODO 暂不支持Cache，又要另外研究
	public List<Fan> queryFansByIdolId(Long idolId, Page page, Activity activity) {
		return getPaginationResult(queryFansByIdolIdSql, page, FanMapper.getMapper(), idolId, activity.toString());
	}

	// TODO 暂不支持Cache，又要另外研究
	public List<Fan> queryIdolsByFanIdAndIdolType(Long fanId, IdolType idolType, Page page, Activity activity) {
		if (idolType == null) {
			return queryIdolsByFanId(fanId, page, activity);
		}
		return getPaginationResult(queryIdolsByFanIdAndIdolTypeSql, page, FanMapper.getMapper(), fanId,
				idolType.toString(), activity.toString());
	}

	// TODO 暂不支持Cache，又要另外研究
	public List<Fan> queryIdolsByFanId(Long fanId, Page page, Activity activity) {
		return getPaginationResult(queryIdolsByFanIdSql, page, FanMapper.getMapper(), fanId, activity.toString());
	}

	public int countFans(Long idolId, Long fanId, Activity activity) {
		return jdbcTemplate.queryForInt(countFandsSql, idolId, fanId, activity.toString());
	}

	public int countNormalFans(Long idolId) {
		return getCountResult(queryFansByIdolIdSql, idolId, Activity.NORMAL.toString());
	}

	public int countNormalIdols(Long fanId, IdolType idolType) {
		return getCountResult(queryIdolsByFanIdAndIdolTypeSql, fanId, idolType.toString(), Activity.NORMAL.toString());
	}

	// TODO 暂不支持Cache，又要另外研究
	public void updateFanActivity(Long idolId, Long fanId, Activity activity) {
		jdbcTemplate.update(updateFanActivitySql, activity.toString(), idolId, fanId);
	}

	// TODO 暂不支持Cache，又要另外研究
	public List<Fan> queryBothIdols(Long ownerId, Long viewerId, Page page, Activity activity) {
		return getPaginationResult(queryBothIdolsSql, page, FanMapper.getMapper(), ownerId, viewerId,
				activity.toString());
	}

	// TODO 暂不支持Cache，又要另外研究
	public List<Fan> queryBothIdolsByIdolType(Long ownerId, Long viewerId, IdolType idolType, Page page,
			Activity activity) {
		if (idolType == null) {
			return queryBothIdols(ownerId, viewerId, page, activity);
		}
		return getPaginationResult(queryBothIdolsByIdolTypeSql, page, FanMapper.getMapper(), ownerId, viewerId,
				idolType.toString(), activity.toString());
	}

	// TODO 暂不支持Cache，又要另外研究
	public List<Fan> queryIdolsWithFollowed(Long ownerId, Long viewerId, Page page, Activity activity) {
		return getPaginationResult(queryIdolsWithFollowedSql, page, FanMapper.getMapper(), ownerId, viewerId, ownerId,
				activity.toString());
	}

	// TODO 暂不支持Cache，又要另外研究
	public List<Fan> queryIdolsByIdolTypeWithFollowed(Long ownerId, Long viewerId, IdolType idolType, Page page,
			Activity activity) {
		if (idolType == null) {
			return queryIdolsWithFollowed(ownerId, viewerId, page, activity);
		}
		return getPaginationResult(queryIdolsByIdolTypeWithFollowedSql, page, FanMapper.getMapper(), ownerId, viewerId,
				ownerId, idolType.toString(), activity.toString());
	}

	// TODO 暂不支持Cache，又要另外研究
	public List<Fan> queryFansWithFollowed(Long idolId, Long viewerId, Page page, Activity activity) {
		return getPaginationResult(queryFansWithFollowedSql, page, FanMapper.getMapper(), idolId, viewerId, idolId,
				activity.toString());
	}

	// TODO 暂不支持Cache，又要另外研究
	public List<Fan> queryEachOtherIdols(Long fanId, Page page, Activity activity) {
		return getPaginationResult(queryEachOtherIdolsSql, page, FanMapper.getMapper(), fanId, activity.toString());
	}

}
